﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api.Ddl;

namespace Me.Prettyprint.Cassandra.Serializers
{
    /// <summary>
    /// UUIDSerializer uses the GuidUtils to handle Guid/UUID conversion.
    /// </summary>
    public class UUIDSerializer : AbstractSerializer<System.Guid>
    {
        public static UUIDSerializer Instance = new UUIDSerializer();

        public UUIDSerializer()
        {
        }
        public override ComparatorType GetComparatorType()
        {
            return ComparatorType.UUIDTYPE;
        }
        public override ComparatorType ComparatorType
        {
            get
            {
                return ComparatorType.UUIDTYPE;
            }
        }
        public override byte[] ToBytes(System.Guid obj)
        {
            var bytes = obj.ToByteArray();
            ReverseLowFieldTimestamp(bytes);
            ReverseMiddleFieldTimestamp(bytes);
            ReverseHighFieldTimestamp(bytes);
            return bytes;
        }
        public override Utils.ByteBuffer ToByteBuffer(System.Guid obj)
        {
            return new ByteBuffer(ToBytes(obj));
        }
        public override System.Guid FromBytes(byte[] bytes)
        {
            var buffer = (byte[])bytes.Clone();
            ReverseLowFieldTimestamp(buffer);
            ReverseMiddleFieldTimestamp(buffer);
            ReverseHighFieldTimestamp(buffer);
            return new Guid(buffer);
        }
        public override System.Guid FromByteBuffer(ByteBuffer buff)
        {
            return FromBytes(buff.GetBuffer());
        }
    }
}
